package com.hxd.otc.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.TextView;

import com.flyco.dialog.widget.base.BottomBaseDialog;
import com.hxd.otc.R;
import com.hxd.otc.base.BaseBean;
import com.hxd.otc.module.coin_deal.bean.DialogCoinTypeBean;
import com.hxd.otc.module.coin_deal.model.CoinDealModel;
import com.hxd.otc.mvp.base.IBaseView;
import com.hxd.otc.net.BaseObserver;
import com.hxd.otc.other.CustomBounceTopEnter;
import com.hxd.otc.other.SlideTopExit;
import com.hxd.otc.utils.DataUtils;
import com.hxd.otc.utils.LogUtils;
import com.hxd.otc.utils.ResourceUtil;
import com.hxd.otc.utils.UIUtils;
import com.hxd.otc.widget.BlocksToast;
import com.hxd.otc.widget.LimitNumberTextWatcher;
import com.hxd.otc.widget.NormalButton;
import com.hxd.otc.widget.TypeWindow;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lzy on 2018/7/9 0009.
 * description ${创建挂单对话框}
 */
public class CreateEntryOrdersDialog extends BottomBaseDialog<CreateEntryOrdersDialog> {

    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_cancel)
    TextView mTvCancel;
    @BindView(R.id.et_single_price)
    EditText mEtSinglePrice;
    @BindView(R.id.tv_single_price_unit)
    TextView mTvSinglePriceUnit;
    @BindView(R.id.et_count)
    EditText mEtCount;
    @BindView(R.id.tv_count_unit)
    TextView mTvCountUnit;
    @BindView(R.id.et_pay_password)
    EditText mEtPayPassword;
    @BindView(R.id.tv_create_entry_order_hint)
    TextView mTvCreateEntryOrderHint;
    @BindView(R.id.btn_confirm)
    NormalButton mBtnConfirm;
    @BindView(R.id.tv_sale_type)
    TextView tvSaleType;
    @BindView(R.id.tv_buy_type)
    TextView tvBuyType;

    private int mCheckTeamPosition;
    private int mBuyPos;

    private final IBaseView mIBaseView;
    String[] saleCoinTypes;//卖出币种类型
    List<DialogCoinTypeBean> buyCoinTypeList;//买入币种类型

    private TypeWindow mTypeSaleWindow;
    private TypeWindow mTypeBuyWindow;


    public CreateEntryOrdersDialog(Context context) {
        super(context);
        mIBaseView = (IBaseView) context;
    }


    @Override
    public View onCreateView() {
        return View.inflate(getContext(), R.layout.dialog_create_entry_orders1, null);
    }


    @Override
    public void onViewCreated(View view) {
        ButterKnife.bind(this, view);
        UIUtils.setPayPasswordEditText(mEtPayPassword);
        mEtSinglePrice.addTextChangedListener(new LimitNumberTextWatcher(mEtSinglePrice, 9, 8));
        mEtCount.addTextChangedListener(new LimitNumberTextWatcher(mEtCount, 9, 4));

    }

    public CreateEntryOrdersDialog setCoinTypes(List<DialogCoinTypeBean> coinTypeBeanList) {
        this.buyCoinTypeList = coinTypeBeanList;
        saleCoinTypes = new String[coinTypeBeanList.size()];
        for (int i = 0; i < coinTypeBeanList.size(); i++) {
            saleCoinTypes[i] = coinTypeBeanList.get(i).sellCoinType;
        }

        return this;
    }

    @Override
    public void setUiBeforShow() {
//        updateEntryOrdersType();
    }

    private void updateEntryOrdersType() {
        mTvCountUnit.setText(saleCoinTypes[mCheckTeamPosition]);
        mTvCreateEntryOrderHint.setText(getContext().getString(R.string.create_entry_order_sale_hint_placeholder, saleCoinTypes[mCheckTeamPosition]));
    }

    @OnClick({R.id.tv_cancel, R.id.btn_confirm, R.id.tv_sale_type, R.id.tv_buy_type})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.btn_confirm:
                onConfirm();
                break;
            case R.id.tv_sale_type:
                showSelectType();
                break;
            case R.id.tv_buy_type:
                if (!TextUtils.isEmpty(tvSaleType.getText().toString()) && tvSaleType.getText().toString().equals(getContext().getString(R.string.wallet_add_contacts_coin_select))) {
                    BlocksToast.get().showToast(R.string.wallet_toast_add_contacts_sale_coin_empty);
                    return;
                }
                showSelectBuyType();
                break;

        }
    }

    private void showSelectType() {
        if (mTypeSaleWindow == null) {
            mTypeSaleWindow = new TypeWindow(getContext(), saleCoinTypes, getContext().getString(R.string.wallet_add_contacts_coin_select),
                    tvSaleType.getWidth(), R.layout.list_item_common_pop_list_left);
            mTypeSaleWindow.setOnItemClickListener(new TypeWindow.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    mCheckTeamPosition = position;
                    mTypeSaleWindow.superDismiss();
                    tvSaleType.setText(saleCoinTypes[position]);
                    updateEntryOrdersType();
                    if (mTypeBuyWindow == null) {
                        showSelectBuyType();
                    }else {
                        tvBuyType.setText(ResourceUtil.getString(R.string.wallet_add_contacts_coin_select));
                        mTypeBuyWindow.setTypes(buyCoinTypeList.get(mCheckTeamPosition).buyCoinType);
                        mTvSinglePriceUnit.setText("");
                        mTypeBuyWindow.show();
                    }

                }
            });
            mTypeSaleWindow.offset(0, 0)
                    .anchorView(tvSaleType)
                    .gravity(Gravity.BOTTOM)
                    .showAnim(new CustomBounceTopEnter())
                    .dismissAnim(new SlideTopExit())
                    .dimEnabled(false);

        }
        tvSaleType.setCompoundDrawables(null, null, ResourceUtil.getDrawable(R.mipmap.my_enter_pull_up), null);

        mTypeSaleWindow.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                tvSaleType.setCompoundDrawables(null, null, ResourceUtil.getDrawable(R.mipmap.my_enter_pull_dwon), null);
            }
        });
        mTypeSaleWindow.show();
    }

    private void showSelectBuyType() {
        if (mTypeBuyWindow == null) {
            mTypeBuyWindow = new TypeWindow(getContext(), buyCoinTypeList.get(mCheckTeamPosition).buyCoinType, getContext().getString(R.string.wallet_add_contacts_coin_select),
                    tvBuyType.getWidth(), R.layout.list_item_common_pop_list_left);
            mTypeBuyWindow.setOnItemClickListener(new TypeWindow.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    mBuyPos = position;
                    mTypeBuyWindow.superDismiss();
                    tvBuyType.setText(buyCoinTypeList.get(mCheckTeamPosition).buyCoinType[mBuyPos]);
                    mTvSinglePriceUnit.setText(buyCoinTypeList.get(mCheckTeamPosition).buyCoinType[mBuyPos]);
                }
            });
            mTypeBuyWindow.offset(0, 0)
                    .anchorView(tvBuyType)
                    .gravity(Gravity.BOTTOM)
                    .showAnim(new CustomBounceTopEnter())
                    .dismissAnim(new SlideTopExit())
                    .dimEnabled(false);

        }
        tvBuyType.setCompoundDrawables(null, null, ResourceUtil.getDrawable(R.mipmap.my_enter_pull_up), null);

        mTypeBuyWindow.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                tvBuyType.setCompoundDrawables(null, null, ResourceUtil.getDrawable(R.mipmap.my_enter_pull_dwon), null);
            }
        });
        mTypeBuyWindow.show();
    }

    private void onConfirm() {

        if (!TextUtils.isEmpty(tvSaleType.getText().toString()) && tvSaleType.getText().toString().equals(getContext().getString(R.string.wallet_add_contacts_coin_select))) {
            BlocksToast.get().showToast(R.string.wallet_toast_add_contacts_sale_coin_empty);
            return;
        }
        if (!TextUtils.isEmpty(tvBuyType.getText().toString()) && tvBuyType.getText().toString().equals(getContext().getString(R.string.wallet_add_contacts_coin_select))) {
            BlocksToast.get().showToast(R.string.wallet_toast_add_contacts_buy_coin_empty);
            return;
        }
        String count = mEtCount.getText().toString().trim();
        if (TextUtils.isEmpty(count)) {
            BlocksToast.get().showToast(R.string.please_enter_the_number_of_transactions);
            mEtCount.requestFocus();
            return;
        }
        String singlePrice = mEtSinglePrice.getText().toString().trim();
        if (TextUtils.isEmpty(singlePrice)) {
            BlocksToast.get().showToast(R.string.please_input_single_price);
            mEtCount.requestFocus();
            return;
        }

        String payPassword = mEtPayPassword.getText().toString().trim();
        if (!DataUtils.isPayPassword(payPassword)) {
            return;
        }

        String coinType = buyCoinTypeList.get(mCheckTeamPosition).sellCoinType + "/" + buyCoinTypeList.get(mCheckTeamPosition).buyCoinType[mBuyPos];
        LogUtils.e("coinType====" + coinType);
        CoinDealModel.getInstance().insertEntryOrders(singlePrice, count, coinType, payPassword, new BaseObserver<String>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull String s) {
                BlocksToast.get().showToast(TextUtils.isEmpty(msg) ? ResourceUtil.getString(R.string.the_registration_form_was_created_successfully) : msg);
                if (onCreateEntryOrdersListener != null) {
                    onCreateEntryOrdersListener.onCreateEntryOrdersSuccess();
                }
                dismiss();
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                BlocksToast.get().showToast(errMessage);
            }
        }, mIBaseView.<BaseBean<String>>bindAutoDispose());
    }


    public void onDestory() {
        if (mTypeSaleWindow != null) {
            mTypeSaleWindow.setOnDismissListener(null);
            if (mTypeSaleWindow.isShowing()) {
                mTypeSaleWindow.dismiss();
            }
            mTypeSaleWindow = null;
        }
        if (mTypeBuyWindow != null) {
            mTypeBuyWindow.setOnDismissListener(null);
            if (mTypeBuyWindow.isShowing()) {
                mTypeBuyWindow.dismiss();
            }
            mTypeBuyWindow = null;
        }
    }

    private OnCreateEntryOrdersListener onCreateEntryOrdersListener;


    public interface OnCreateEntryOrdersListener {
        void onCreateEntryOrdersSuccess();
    }

    public CreateEntryOrdersDialog setOnCreateEntryOrdersListener(OnCreateEntryOrdersListener onCreateEntryOrdersListener) {
        this.onCreateEntryOrdersListener = onCreateEntryOrdersListener;
        return this;
    }
}
